package com.ugoodtech.mdcc.client.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.service.ComplaintUserService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.ComplaintReport;
import com.ugoodtech.mdcc.core.domain.ComplaintUser;
import com.ugoodtech.mdcc.core.domain.QComplaintUser;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.repository.ComplaintUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Date;

@Service
public class ComplaintUserServiceImpl implements ComplaintUserService {

    @Autowired
    private ComplaintUserRepository complaintUserRepository;

    @Override
    public void updateComplaintUser(User user, ComplaintReport complaintReport) {
        deleteEmptyComplaintUser(complaintReport);
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintUser qComplaintUser = QComplaintUser.complaintUser;
        builder.and(qComplaintUser.deleted.eq(false));
        builder.and(qComplaintUser.enabled.eq(true));
        builder.and(qComplaintUser.complaintReport.id.eq(complaintReport.getId()));
        builder.and(qComplaintUser.user.id.eq(user.getId()));
        ComplaintUser complaintUser = complaintUserRepository.findOne(builder);
        if (null == complaintUser) {
            complaintUser = new ComplaintUser();
            complaintUser.setComplaintReport(complaintReport);
            complaintUser.setUser(user);
        }
        Long auditTime = complaintReport.getAuditTime().getTime();
        Long nowTime = System.currentTimeMillis();
        double useTime = 0.0;
        if (complaintReport.getReportStatus().equals(Constants.COMPLETE) || complaintReport.getReportStatus().equals(Constants.STOP)) {
            Long solutionTime = complaintReport.getSolutionTime().getTime();
            useTime = solutionTime - auditTime;
        } else {
            useTime = nowTime - auditTime;
        }
        useTime = useTime / 1000 / 60 / 60;
        if (null != complaintUser.getUseTime()) {
            double logUseTime = Double.valueOf(complaintUser.getUseTime());
            complaintUser.setUseTime(getDecimalDouble(useTime + logUseTime));
        } else {
            complaintUser.setUseTime(getDecimalDouble(useTime));
        }
        complaintUser.setUpdateTime(new Date());
        complaintUser.setType(complaintReport.getReportStatus());
        complaintUserRepository.save(complaintUser);
    }

    private static String getDecimalDouble(double doubleValue) {
        DecimalFormat df = new DecimalFormat("0.0");
        return df.format(doubleValue);
    }

    @Override
    public void addComplaintUser(ComplaintReport complaintReport) {
        deleteEmptyComplaintUser(complaintReport);
        ComplaintUser complaintUser = new ComplaintUser();
        complaintUser.setComplaintReport(complaintReport);
        complaintUserRepository.save(complaintUser);
    }

    private void deleteEmptyComplaintUser(ComplaintReport complaintReport) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintUser qComplaintUser = QComplaintUser.complaintUser;
        builder.and(qComplaintUser.deleted.eq(false));
        builder.and(qComplaintUser.enabled.eq(true));
        builder.and(qComplaintUser.complaintReport.id.eq(complaintReport.getId()));
        builder.and(qComplaintUser.user.isNull());
        builder.and(qComplaintUser.timeOut.eq(false));
        builder.and(qComplaintUser.type.isNull());
        builder.and(qComplaintUser.useTime.isNull());
        ComplaintUser complaintUser = complaintUserRepository.findOne(builder);
        if (null != complaintUser) {
            complaintUserRepository.delete(complaintUser.getId());
        }
    }


}
